<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>bezier.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_math_bezier.js.html">bezier.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview Represents a cubic Bezier curve.
<a name="line18"></a> *
<a name="line19"></a> * Uses the deCasteljau algorithm to compute points on the curve.
<a name="line20"></a> * http://en.wikipedia.org/wiki/De_Casteljau&#39;s_algorithm
<a name="line21"></a> *
<a name="line22"></a> * Currently it uses an unrolled version of the algorithm for speed.  Eventually
<a name="line23"></a> * it may be useful to use the loop form of the algorithm in order to support
<a name="line24"></a> * curves of arbitrary degree.
<a name="line25"></a> *
<a name="line26"></a> */
<a name="line27"></a>
<a name="line28"></a>goog.provide(&#39;goog.math.Bezier&#39;);
<a name="line29"></a>
<a name="line30"></a>goog.require(&#39;goog.math&#39;);
<a name="line31"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Object representing a cubic bezier curve.
<a name="line36"></a> * @param {number} x0 X coordinate of the start point.
<a name="line37"></a> * @param {number} y0 Y coordinate of the start point.
<a name="line38"></a> * @param {number} x1 X coordinate of the first control point.
<a name="line39"></a> * @param {number} y1 Y coordinate of the first control point.
<a name="line40"></a> * @param {number} x2 X coordinate of the second control point.
<a name="line41"></a> * @param {number} y2 Y coordinate of the second control point.
<a name="line42"></a> * @param {number} x3 X coordinate of the end point.
<a name="line43"></a> * @param {number} y3 Y coordinate of the end point.
<a name="line44"></a> * @constructor
<a name="line45"></a> */
<a name="line46"></a>goog.math.Bezier = function(x0, y0, x1, y1, x2, y2, x3, y3) {
<a name="line47"></a>  /**
<a name="line48"></a>   * X coordinate of the first point.
<a name="line49"></a>   * @type {number}
<a name="line50"></a>   */
<a name="line51"></a>  this.x0 = x0;
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * Y coordinate of the first point.
<a name="line55"></a>   * @type {number}
<a name="line56"></a>   */
<a name="line57"></a>  this.y0 = y0;
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * X coordinate of the first control point.
<a name="line61"></a>   * @type {number}
<a name="line62"></a>   */
<a name="line63"></a>  this.x1 = x1;
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * Y coordinate of the first control point.
<a name="line67"></a>   * @type {number}
<a name="line68"></a>   */
<a name="line69"></a>  this.y1 = y1;
<a name="line70"></a>
<a name="line71"></a>  /**
<a name="line72"></a>   * X coordinate of the second control point.
<a name="line73"></a>   * @type {number}
<a name="line74"></a>   */
<a name="line75"></a>  this.x2 = x2;
<a name="line76"></a>
<a name="line77"></a>  /**
<a name="line78"></a>   * Y coordinate of the second control point.
<a name="line79"></a>   * @type {number}
<a name="line80"></a>   */
<a name="line81"></a>  this.y2 = y2;
<a name="line82"></a>
<a name="line83"></a>  /**
<a name="line84"></a>   * X coordinate of the end point.
<a name="line85"></a>   * @type {number}
<a name="line86"></a>   */
<a name="line87"></a>  this.x3 = x3;
<a name="line88"></a>
<a name="line89"></a>  /**
<a name="line90"></a>   * Y coordinate of the end point.
<a name="line91"></a>   * @type {number}
<a name="line92"></a>   */
<a name="line93"></a>  this.y3 = y3;
<a name="line94"></a>};
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Constant used to approximate ellipses.
<a name="line99"></a> * See: http://canvaspaint.org/blog/2006/12/ellipse/
<a name="line100"></a> * @type {number}
<a name="line101"></a> */
<a name="line102"></a>goog.math.Bezier.KAPPA = 4 * (Math.sqrt(2) - 1) / 3;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * @return {goog.math.Bezier} A copy of this curve.
<a name="line107"></a> */
<a name="line108"></a>goog.math.Bezier.prototype.clone = function() {
<a name="line109"></a>  return new goog.math.Bezier(this.x0, this.y0, this.x1, this.y1, this.x2,
<a name="line110"></a>      this.y2, this.x3, this.y3);
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Test if the given curve is exactly the same as this one.
<a name="line116"></a> * @param {goog.math.Bezier} other The other curve.
<a name="line117"></a> * @return {boolean} Whether the given curve is the same as this one.
<a name="line118"></a> */
<a name="line119"></a>goog.math.Bezier.prototype.equals = function(other) {
<a name="line120"></a>  return this.x0 == other.x0 &amp;&amp; this.y0 == other.y0 &amp;&amp; this.x1 == other.x1 &amp;&amp;
<a name="line121"></a>         this.y1 == other.y1 &amp;&amp; this.x2 == other.x2 &amp;&amp; this.y2 == other.y2 &amp;&amp;
<a name="line122"></a>         this.x3 == other.x3 &amp;&amp; this.y3 == other.y3;
<a name="line123"></a>};
<a name="line124"></a>
<a name="line125"></a>
<a name="line126"></a>/**
<a name="line127"></a> * Modifies the curve in place to progress in the opposite direction.
<a name="line128"></a> */
<a name="line129"></a>goog.math.Bezier.prototype.flip = function() {
<a name="line130"></a>  var temp = this.x0;
<a name="line131"></a>  this.x0 = this.x3;
<a name="line132"></a>  this.x3 = temp;
<a name="line133"></a>  temp = this.y0;
<a name="line134"></a>  this.y0 = this.y3;
<a name="line135"></a>  this.y3 = temp;
<a name="line136"></a>
<a name="line137"></a>  temp = this.x1;
<a name="line138"></a>  this.x1 = this.x2;
<a name="line139"></a>  this.x2 = temp;
<a name="line140"></a>  temp = this.y1;
<a name="line141"></a>  this.y1 = this.y2;
<a name="line142"></a>  this.y2 = temp;
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Computes the curve at a point between 0 and 1.
<a name="line148"></a> * @param {number} t The point on the curve to find.
<a name="line149"></a> * @return {goog.math.Coordinate} The computed coordinate.
<a name="line150"></a> */
<a name="line151"></a>goog.math.Bezier.prototype.getPoint = function(t) {
<a name="line152"></a>  // Special case start and end
<a name="line153"></a>  if (t == 0) {
<a name="line154"></a>    return new goog.math.Coordinate(this.x0, this.y0);
<a name="line155"></a>  } else if (t == 1) {
<a name="line156"></a>    return new goog.math.Coordinate(this.x3, this.y3);
<a name="line157"></a>  }
<a name="line158"></a>
<a name="line159"></a>  // Step one - from 4 points to 3
<a name="line160"></a>  var ix0 = goog.math.lerp(this.x0, this.x1, t);
<a name="line161"></a>  var iy0 = goog.math.lerp(this.y0, this.y1, t);
<a name="line162"></a>
<a name="line163"></a>  var ix1 = goog.math.lerp(this.x1, this.x2, t);
<a name="line164"></a>  var iy1 = goog.math.lerp(this.y1, this.y2, t);
<a name="line165"></a>
<a name="line166"></a>  var ix2 = goog.math.lerp(this.x2, this.x3, t);
<a name="line167"></a>  var iy2 = goog.math.lerp(this.y2, this.y3, t);
<a name="line168"></a>
<a name="line169"></a>  // Step two - from 3 points to 2
<a name="line170"></a>  ix0 = goog.math.lerp(ix0, ix1, t);
<a name="line171"></a>  iy0 = goog.math.lerp(iy0, iy1, t);
<a name="line172"></a>
<a name="line173"></a>  ix1 = goog.math.lerp(ix1, ix2, t);
<a name="line174"></a>  iy1 = goog.math.lerp(iy1, iy2, t);
<a name="line175"></a>
<a name="line176"></a>  // Final step - last point
<a name="line177"></a>  return new goog.math.Coordinate(goog.math.lerp(ix0, ix1, t),
<a name="line178"></a>      goog.math.lerp(iy0, iy1, t));
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Changes this curve in place to be the portion of itself from [t, 1].
<a name="line184"></a> * @param {number} t The start of the desired portion of the curve.
<a name="line185"></a> */
<a name="line186"></a>goog.math.Bezier.prototype.subdivideLeft = function(t) {
<a name="line187"></a>  if (t == 1) {
<a name="line188"></a>    return;
<a name="line189"></a>  }
<a name="line190"></a>
<a name="line191"></a>  // Step one - from 4 points to 3
<a name="line192"></a>  var ix0 = goog.math.lerp(this.x0, this.x1, t);
<a name="line193"></a>  var iy0 = goog.math.lerp(this.y0, this.y1, t);
<a name="line194"></a>
<a name="line195"></a>  var ix1 = goog.math.lerp(this.x1, this.x2, t);
<a name="line196"></a>  var iy1 = goog.math.lerp(this.y1, this.y2, t);
<a name="line197"></a>
<a name="line198"></a>  var ix2 = goog.math.lerp(this.x2, this.x3, t);
<a name="line199"></a>  var iy2 = goog.math.lerp(this.y2, this.y3, t);
<a name="line200"></a>
<a name="line201"></a>  // Collect our new x1 and y1
<a name="line202"></a>  this.x1 = ix0;
<a name="line203"></a>  this.y1 = iy0;
<a name="line204"></a>
<a name="line205"></a>  // Step two - from 3 points to 2
<a name="line206"></a>  ix0 = goog.math.lerp(ix0, ix1, t);
<a name="line207"></a>  iy0 = goog.math.lerp(iy0, iy1, t);
<a name="line208"></a>
<a name="line209"></a>  ix1 = goog.math.lerp(ix1, ix2, t);
<a name="line210"></a>  iy1 = goog.math.lerp(iy1, iy2, t);
<a name="line211"></a>
<a name="line212"></a>  // Collect our new x2 and y2
<a name="line213"></a>  this.x2 = ix0;
<a name="line214"></a>  this.y2 = iy0;
<a name="line215"></a>
<a name="line216"></a>  // Final step - last point
<a name="line217"></a>  this.x3 = goog.math.lerp(ix0, ix1, t);
<a name="line218"></a>  this.y3 = goog.math.lerp(iy0, iy1, t);
<a name="line219"></a>};
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * Changes this curve in place to be the portion of itself from [0, t].
<a name="line224"></a> * @param {number} t The end of the desired portion of the curve.
<a name="line225"></a> */
<a name="line226"></a>goog.math.Bezier.prototype.subdivideRight = function(t) {
<a name="line227"></a>  this.flip();
<a name="line228"></a>  this.subdivideLeft(1 - t);
<a name="line229"></a>  this.flip();
<a name="line230"></a>};
<a name="line231"></a>
<a name="line232"></a>
<a name="line233"></a>/**
<a name="line234"></a> * Changes this curve in place to be the portion of itself from [s, t].
<a name="line235"></a> * @param {number} s The start of the desired portion of the curve.
<a name="line236"></a> * @param {number} t The end of the desired portion of the curve.
<a name="line237"></a> */
<a name="line238"></a>goog.math.Bezier.prototype.subdivide = function(s, t) {
<a name="line239"></a>  this.subdivideRight(s);
<a name="line240"></a>  this.subdivideLeft((t - s) / (1 - s));
<a name="line241"></a>};
</pre>


</body>
</html>
